package com.lecing.data.handler;

import com.lecing.data.entity.Community;
import com.lecing.fetch.util.DBUtil;
import com.lecing.fetch.util.FetchApp;
import com.lecing.fetch.util.HrefParseUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * 处理小区数据并入库
 * Created by Administrator on 2018/5/11.
 */
public class CommunityHandler {

   static Log log= LogFactory.getLog(CommunityHandler.class);

    public  static void main(String[] args) {
        /***/
        String accUrl="http://xa.anjuke.com/community/xinchengqu/p";
        batchHandler(accUrl,1,10,"4001");

    }

    /**
     *  批量抓取安居客小区数据
     * @param fetchUrl 要抓取的网页数据URL地址
     * @param startPage 抓取列表开始页
     * @param pageNum   抓取列表页数
     * @param cityCode  城市代码
     */
    private static void batchHandler(String fetchUrl,int startPage,int pageNum,String cityCode){
        log.info("=======抓取数据开始=======");
        for(int i=startPage;i<pageNum;i++){
           log.info("插入页的url："+fetchUrl+i+"");
            String inUrl=fetchUrl+i;
            doInsertData4PageNo(inUrl,cityCode);
            inUrl="";
            log.info("插入第"+i+"页数据完成！");
        }
        log.info("=======抓取数据入库完成=======");
    }
    /**
     * 执行批量数据插入到库
     * @param accUrl http://xa.anjuke.com/community/p
     * @param pageSize 页面数
     */
    private static void doInsertData(String accUrl,Integer pageSize,String cityCode){
        List<String> urlList=getPageArr(accUrl,pageSize);
        try {
            //1.获取页面源码
            for(String url:urlList) {
                String pageCode = FetchApp.getHtmlByUrl(url);
                //2.解析源码到list集合
                List<String> dataList = FetchApp.parserHtml(pageCode);
                // System.out.print("小区大小："+dataList.size());
                //循环遍历插入DB
                for (String str : dataList) {
                    Community community = HrefParseUtil.parseHrefStr(str, cityCode);
                    int result = DBUtil.insert(community);
                    //   System.out.println("保存结果：" + result);
                }
                Thread.currentThread().sleep(3000);
            }
        }catch (Exception e){
            System.err.print("数据插入出错！"+e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 指定保存的页面数据
     * @param accUrl 传入带有分页号的url 如：http://xa.anjuke.com/community/p13
     */
    private static void doInsertData4PageNo(String accUrl,String cityCode){
        try {
                //1.获取页面源码
                String pageCode = FetchApp.getHtmlByUrl(accUrl);
                //2.解析源码到list集合
                List<String> dataList = FetchApp.parserHtml(pageCode);
                // System.out.print("小区大小："+dataList.size());
                //循环遍历插入DB
                for (String str : dataList) {
                    Community community = HrefParseUtil.parseHrefStr(str, cityCode);
                    int result = DBUtil.insert(community);
                     System.out.println("保存结果：" + result);
                }

        }catch (Exception e){
            System.err.print("数据插入出错！"+e.getMessage());
            e.printStackTrace();
        }
    }
    /**
     * 获取的访问url集合
     * @param url
     * @param size
     * @return
     */
    private static List<String> getPageArr(String url,int size){
        Integer[] pageArr = getPageNo(size);
        List<String> urls=new ArrayList<>();
        for(int i=1;i<pageArr.length;i++){
            StringBuilder sb=new StringBuilder(url);
            sb.append(i);
            System.out.println(sb.toString());
            urls.add(sb.toString());
            sb=null;
        }
        return urls;
    }

    /**
     * 生成指定长度的数组
     * @param pageSum
     * @return
     */
    private static Integer[] getPageNo(int pageSum){
        Integer[] pageArr = new Integer[pageSum];
        for(int i=1;i<pageArr.length;i++) {
            pageArr[i]=i;
        }
        return pageArr;
    }
}
